/*
温度计使用Si7021芯片，oled显示
*/

#include <Wire.h>
#include <Adafruit_ssd1306syp.h>
#include "SevSeg.h"
#define SDA_PIN 8
#define SCL_PIN 9
#define ADDRESS_SI7021 0x40
#define MEASURE_RH_HOLD 0xE5
#define READ_T_FROM_PRE_RH_MEASUREMENT 0xE0
SevSeg sevseg;
byte buffer[] = {0, 0, 0};
byte crcHumi;

word outHumi = 0;
word outTemp = 0;
float valueHumi = 0;
float valueTemp = 0;

byte numDigits = 4;


int numToShow = 0;
int delaycount = 0;
Adafruit_ssd1306syp display(SDA_PIN,SCL_PIN);
void setup()
{
    Wire.begin();
    Serial.begin(9600);
    display.initialize();
}

void loop()
{
    //循环读取湿度数值
    Wire.beginTransmission(ADDRESS_SI7021);
    Wire.write(MEASURE_RH_HOLD);
    Wire.endTransmission();

    Wire.requestFrom(ADDRESS_SI7021, 3);
    if(Wire.available() >= 3)
    {
        buffer[0] = Wire.read(); //高字节
        buffer[1] = Wire.read(); //低字节
        buffer[2] = Wire.read(); //crc校验
    }
    outHumi = (buffer[0]<<8) | buffer[1];
    crcHumi = CRC8(buffer, 3);
    
    //读取温度数值
    Wire.beginTransmission(ADDRESS_SI7021);
    Wire.write(READ_T_FROM_PRE_RH_MEASUREMENT);
    Wire.endTransmission();

    Wire.requestFrom(ADDRESS_SI7021, 2);
    if(Wire.available() >= 2)
    {
        buffer[0] = Wire.read(); //high byte
        buffer[1] = Wire.read(); //low byte; no crc
    }
    outTemp = (buffer[0]<<8) | buffer[1];

    valueHumi = 125.0*outHumi/65536 - 6;
    valueTemp = 175.72*outTemp/65536 - 46.85;

    display.update();
    delay(1000);
    display.clear();
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(0,0);
    display.print("humi:");display.println(valueHumi);//在oled屏上进行显示湿度
    display.print("temp:");display.println(valueTemp);//在oled屏上进行显示温度
    display.setTextColor(BLACK, WHITE);
   

}

byte CRC8(byte *data, byte len)
{
    byte crc[8];
    byte result = 0;
    byte i;
    byte doInvert;
    String bitString = MakeString(data, len);

    for (i = 0; i < 8; i++)
    {
        crc[i] = 0;
    }

    for (i = 0; i < bitString.length(); i ++)
    {
        doInvert = (byte(bitString[i] == '1')) ^ crc[7];
        crc[7] = crc[6];
        crc[6] = crc[5];
        crc[5] = crc[4] ^ doInvert;
        crc[4] = crc[3] ^ doInvert;
        crc[3] = crc[2];
        crc[2] = crc[1];
        crc[1] = crc[0];
        crc[0] = doInvert;
    }

    for (i = 0; i < 8; i++)
    {
        bitWrite(result, i, crc[i]);
    }

    return result;
}

String MakeString(byte *data, byte len)
{
    int i, j;
    String tempString[len];
    String bitString="";

    for (i = 0; i < len; i++)
    {
        tempString[i] = String(data[i], BIN);
        int zeros = 8 - tempString[i].length();
        for (j = 0; j < zeros; j++)
        {
            tempString[i] = '0' + tempString[i];
        }
    }

    for (i = 0; i < len; i++)
    {
        bitString = bitString + tempString[i];
    }

    return bitString;
}

